package cn.iocoder.yudao.module.station.controller.admin.cylinder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.station.controller.admin.cylinder.vo.*;
import cn.iocoder.yudao.module.station.dal.dataobject.cylinder.CylinderDO;
import cn.iocoder.yudao.module.station.convert.cylinder.CylinderConvert;
import cn.iocoder.yudao.module.station.service.cylinder.CylinderService;

@Tag(name = "管理后台 - 钢瓶档案")
@RestController
@RequestMapping("/station/cylinder")
@Validated
public class CylinderController {

    @Resource
    private CylinderService cylinderService;

    @PostMapping("/create")
    @Operation(summary = "创建钢瓶档案")
    @PreAuthorize("@ss.hasPermission('station:cylinder:create')")
    public CommonResult<Long> createCylinder(@Valid @RequestBody CylinderCreateReqVO createReqVO) {
        return success(cylinderService.createCylinder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新钢瓶档案")
    @PreAuthorize("@ss.hasPermission('station:cylinder:update')")
    public CommonResult<Boolean> updateCylinder(@Valid @RequestBody CylinderUpdateReqVO updateReqVO) {
        cylinderService.updateCylinder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除钢瓶档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('station:cylinder:delete')")
    public CommonResult<Boolean> deleteCylinder(@RequestParam("id") Long id) {
        cylinderService.deleteCylinder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得钢瓶档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('station:cylinder:query')")
    public CommonResult<CylinderRespVO> getCylinder(@RequestParam("id") Long id) {
        CylinderDO cylinder = cylinderService.getCylinder(id);
        return success(CylinderConvert.INSTANCE.convert(cylinder));
    }

    @GetMapping("/list")
    @Operation(summary = "获得钢瓶档案列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('station:cylinder:query')")
    public CommonResult<List<CylinderRespVO>> getCylinderList(@RequestParam("ids") Collection<Long> ids) {
        List<CylinderDO> list = cylinderService.getCylinderList(ids);
        return success(CylinderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得钢瓶档案分页")
    @PreAuthorize("@ss.hasPermission('station:cylinder:query')")
    public CommonResult<PageResult<CylinderRespVO>> getCylinderPage(@Valid CylinderPageReqVO pageVO) {
        PageResult<CylinderDO> pageResult = cylinderService.getCylinderPage(pageVO);
        return success(CylinderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出钢瓶档案 Excel")
    @PreAuthorize("@ss.hasPermission('station:cylinder:export')")
    @OperateLog(type = EXPORT)
    public void exportCylinderExcel(@Valid CylinderExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CylinderDO> list = cylinderService.getCylinderList(exportReqVO);
        // 导出 Excel
        List<CylinderExcelVO> datas = CylinderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "钢瓶档案.xls", "数据", CylinderExcelVO.class, datas);
    }

}
